﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class administrator_products : System.Web.UI.Page
{
    dulieuDataContext db=new dulieuDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) return;
        LoadNhomCha();
        LoadNhomCon();
        LoadProduct();
    }
    protected void drdnhomcha_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadNhomCon();
    }
    protected void LoadNhomCha()
    {
        drdnhomcha.DataSource = from o in db.Cates where o.TypeCateID == 2 && o.Parent == 0 select o;
        drdnhomcha.DataTextField = "Name";
        drdnhomcha.DataValueField = "Cate_ID";
        drdnhomcha.DataBind();
    }
    protected void LoadNhomCon()
    {
        drdnhomcon.DataSource = from o in db.Cates
                                where o.TypeCateID == 2 && o.Parent == Convert.ToInt32(drdnhomcha.SelectedValue)
                                select o;
        drdnhomcon.DataTextField = "Name";
        drdnhomcon.DataValueField = "Cate_ID";
        drdnhomcon.DataBind();
    }
    protected void LoadProduct()
    {
        DataTable dtSource = new DataTable();
        dtSource.Columns.Add("ProductID");
        dtSource.Columns.Add("Name");
        dtSource.Columns.Add("Image");
        dtSource.Columns.Add("Descriptions");
        dtSource.Columns.Add("Price");
        dtSource.Columns.Add("PriceOnline");
        dtSource.Columns.Add("Quantity");
        dtSource.Columns.Add("Daban");
        dtSource.Columns.Add("Conlai");

        List<Product> dbList = (from o in db.Products where o.Cate_ID == Convert.ToInt32(drdnhomcon.SelectedValue) select o).ToList();
        foreach (Product item in dbList)
        {

            var oRow = dtSource.Rows.Add();
            oRow["ProductID"] = item.ProductID;
            oRow["Name"] = item.Name;
            oRow["Image"] = item.Image;
            oRow["Descriptions"] = item.Descriptions;
            oRow["Price"] = item.Price;
            oRow["PriceOnline"] = item.PriceOnline;
            oRow["Quantity"] = item.Quantity;
            int iDaban = db.OrderDetails.Where(a => a.ProductID == item.ProductID).Count();
            oRow["Daban"] = iDaban;
            oRow["Conlai"] = item.Quantity - iDaban;

            //int soluong = 0;
            //var daban = (from o in db.OrderDetails where o.ProductID == item.ProductID select o.Quantity);
            //foreach (int xt in daban)
            //    soluong += xt;

            //int Slconlai = 0;
            //var conlai = (from o in db.OrderDetails where o.ProductID == item.ProductID select o.Quantity);
            //foreach (int cl in conlai)
            //    Slconlai = soluong - cl;

            //dtSource.Rows.Add(item.ProductID, item.Name, item.Image, item.Descriptions, item.Price, item.PriceOnline, item.Quantity, soluong,Slconlai);
        }
        rpt_product.DataSource = dtSource;
        rpt_product.DataBind();
        //rpt_product.DataSource = from o in db.Products where o.Cate_ID == Convert.ToInt32(drdnhomcon.SelectedValue) select o;
        //rpt_product.DataBind();
    }
    protected void DeleteMenu(object sender, ImageClickEventArgs e)
    {
        foreach (RepeaterItem item in rpt_product.Items)
        {
            var chk = (CheckBox) item.FindControl("chk");
            var lblid = (Label) item.FindControl("lblid");
            if (!chk.Checked) continue;
            var product =
                (from o in db.Products where o.ProductID == (Convert.ToDecimal(lblid.Text)) select o).FirstOrDefault
                    ();
            if (product != null) db.Products.DeleteOnSubmit(product);
            db.SubmitChanges();
        }
        lblthongbao.Text = "Xóa thành công";
        LoadProduct();
        
    }
    protected void drdnhomcon_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadProduct();
    }
        protected void UpdateCheck(object s,RepeaterCommandEventArgs e)
        {
            if(e.CommandName=="edit")
            {
                var lblid = (Label) e.Item.FindControl("lblid");
                Session.Add("id_product",lblid.Text);
                Response.Redirect("update-product.aspx");
            }
        }
}